package com.mz.ozy.mvp.presenter;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;

import com.mz.ozy.base.BasePresenterImpl;
import com.mz.ozy.bean.FamilyFeastBean;
import com.mz.ozy.bean.OnlineFamilyBean;
import com.mz.ozy.bean.OnlineListBean;
import com.mz.ozy.bean.OrderBean;
import com.mz.ozy.constants.Constants;
import com.mz.ozy.mvp.model.PrivateCustomModel;
import com.mz.ozy.mvp.view.PrivateCustomContract;
import com.mz.ozy.net.MyObserver;
import com.mz.ozy.net.RetrofitUtils;
import com.mz.ozy.utils.JsonUtils;
import com.mz.ozy.utils.MD5Util;
import com.mz.ozy.utils.RxUtil;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import okhttp3.ResponseBody;

/**
 * @author lifeStyle on 2020/4/26 0026.
 */
public class PrivateCustomPresenter extends BasePresenterImpl<PrivateCustomContract.View> implements PrivateCustomContract.Presenter {
    private PrivateCustomModel shogModel;

    public PrivateCustomPresenter() {
        super();
        shogModel = new PrivateCustomModel();
    }


    @Override
    public void geAssetsInfo(Activity activity, String city, String address, boolean isShow) {
        shogModel.geAssetsInfo(activity, city, address)
                .compose(RxUtil.rxSchedulerHelper(mView.<ResponseBody>bindToLifecycle()))
                .subscribe(new MyObserver<FamilyFeastBean>(mView, isShow) {
                    @Override
                    public void onSuccesss(FamilyFeastBean bean) {
                        mView.geAssetsInfoSuccess(bean);
                    }
                });
    }

    @Override
    public void getInfo(Activity activity, String city, String area, String id, String gid) {
        shogModel.getInfo(activity, city, area, id, gid)
                .compose(RxUtil.rxSchedulerHelper(mView.<ResponseBody>bindToLifecycle()))
                .subscribe(new MyObserver<OnlineFamilyBean>(mView, false) {
                    @Override
                    public void onSuccesss(OnlineFamilyBean homeMerBean) {
                        mView.getInfo(homeMerBean);
                    }
                });
    }

    public void onBookPrivate(Context context, String id, String attribute_id, String cardid, String cid, String bid, String yman, String mealdata, String mealtime, String gender, String realName, String phone, String remark) {
        shogModel.onBookPrivate(context, id, attribute_id, cardid, cid, bid, yman, mealdata, mealtime, gender, realName, phone, remark)
                .compose(RxUtil.rxSchedulerHelper(mView.<ResponseBody>bindToLifecycle()))
                .subscribe(new MyObserver<OrderBean>(mView, true) {
                    @Override
                    public void onSuccesss(OrderBean homeMerBean) {
                        mView.onBookPrivateSuccess(homeMerBean);
                    }
                });
    }


    @Override
    public void onPay(Context contex, String orderId, String type, String pwd, int familyType) {
        if (familyType == 0) {
            Map<String, String> maps = new HashMap<>();
            maps.put("id", orderId);
            maps.put("payType", type);
            maps.put("payPwd", MD5Util.getMD5String(pwd));
            RetrofitUtils.getApiService(contex).post(Constants.TransCode.TransCode10094, JsonUtils.requestJsonDate(maps), Constants.tokenId)
                    .compose(RxUtil.rxSchedulerHelper(mView.<ResponseBody>bindToLifecycle()))
                    .subscribe(new MyObserver<JSONObject>(mView, true) {

                        @Override
                        public void onSuccesss(JSONObject orderBean) {
                            mView.onPaySuccess(orderBean);
                        }

                        @Override
                        public void onError(Throwable e) {
                            super.onError(e);
                            mView.onPayError("");
                        }
                    }.setTransCode(Constants.TransCode.TransCode10094));
        } else {
            Map<String, String> maps = new HashMap<>();
            maps.put("id", orderId);
            maps.put("payType", type);
            maps.put("payPwd", MD5Util.getMD5String(pwd));
            RetrofitUtils.getApiService(contex).post(Constants.TransCode.TransCode10091, JsonUtils.requestJsonDate(maps), Constants.tokenId)
                    .compose(RxUtil.rxSchedulerHelper(mView.<ResponseBody>bindToLifecycle()))
                    .subscribe(new MyObserver<JSONObject>(mView, true) {

                        @Override
                        public void onSuccesss(JSONObject orderBean) {
                            mView.onPaySuccess(orderBean);
                        }

                        @Override
                        public void onError(Throwable e) {
                            super.onError(e);
                            mView.onPayError("");
                        }
                    }.setTransCode(Constants.TransCode.TransCode10091));
        }

    }

    public void queryOnlineOrder(Context context, String page) {
        Map<String, String> maps = new HashMap<>();
        maps.put("pageNo", page);
        maps.put("pageSize", Constants.PageSize);
        RetrofitUtils.getApiService(context).post(Constants.TransCode.TransCode10095, JsonUtils.requestJsonDate(maps), Constants.tokenId)
                .compose(RxUtil.rxSchedulerHelper(mView.<ResponseBody>bindToLifecycle()))
                .subscribe(new MyObserver<OnlineListBean>(mView, true) {

                    @Override
                    public void onSuccesss(OnlineListBean orderBean) {
                        mView.queryOnlineOrderSuccess(orderBean);
                    }
                });
    }

    public void queryHomeOrder(Context context, String state, String page) {
        Map<String, String> maps = new HashMap<>();
        if (!TextUtils.isEmpty(state)) {
            maps.put("state", state);
        }
        maps.put("pageNo", page);
        maps.put("pageSize", Constants.PageSize);
        RetrofitUtils.getApiService(context).post(Constants.TransCode.TransCode10302, JsonUtils.requestJsonDate(maps), Constants.tokenId)
                .compose(RxUtil.rxSchedulerHelper(mView.<ResponseBody>bindToLifecycle()))
                .subscribe(new MyObserver<OnlineListBean>(mView, true) {

                    @Override
                    public void onSuccesss(OnlineListBean orderBean) {
                        mView.queryHomeOrderSuccess(orderBean);
                    }
                });
    }

    public void cancelBook(Context context, String id) {
        Map<String, String> maps = new HashMap<>();
        maps.put("id", id);
        RetrofitUtils.getApiService(context).post(Constants.TransCode.TransCode10097, JsonUtils.requestJsonDate(maps), Constants.tokenId)
                .compose(RxUtil.rxSchedulerHelper(mView.<ResponseBody>bindToLifecycle()))
                .subscribe(new MyObserver<OnlineListBean>(mView, true) {

                    @Override
                    public void onSuccesss(OnlineListBean orderBean) {
                        mView.cancelBookSuccess(orderBean);
                    }
                });
    }

    public void cancelBookHome(Context context, String id) {
        Map<String, String> maps = new HashMap<>();
        maps.put("tesq", id);
        RetrofitUtils.getApiService(context).post(Constants.TransCode.TransCode10303, JsonUtils.requestJsonDate(maps), Constants.tokenId)
                .compose(RxUtil.rxSchedulerHelper(mView.<ResponseBody>bindToLifecycle()))
                .subscribe(new MyObserver<OnlineListBean>(mView, true) {

                    @Override
                    public void onSuccesss(OnlineListBean orderBean) {
                        mView.cancelBookHomeSuccess(orderBean);
                    }
                });
    }
}
